package com.sdry.web.filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.sdry.entity.base.Staff;
import com.sdry.utils.StringUtil;
/**
 * 
 * @ClassName:LoginInterceptor
 * @Description:拦截器：拦截  .do  路径
 * @Author tdd
 * @Date 2018年5月15日下午4:36:47
 * @version 1.0
 */
@Controller
public class LoginInterceptor implements HandlerInterceptor {
	//进入 Handler方法之前执行
	//用于身份认证、身份授权
	//比如身份认证，如果认证通过表示当前用户没有登陆，需要此方法拦截不再向下执行
	public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
		//获取请求的url
        String url = request.getRequestURI();
        //判断url是否是公开 地址（实际使用时将公开 地址配置配置文件中）//这里公开地址是登陆提交的地址
        //登陆
        if(url.indexOf("login/login.do")>=0){
        	//如果进行登陆提交，放行
        	return true;
        }
        //跳转到登陆页面
        if(url.indexOf("login/toLogin.do")>=0){
        	//如果进行登陆提交，放行
        	return true;
        }

        //判断session
        HttpSession session  = request.getSession();
        //从session中取出用户身份信息
        Staff staffSession = (Staff) session.getAttribute("staff");
       
        if(staffSession != null){
            //身份存在，放行
        	return true;
        }
        
		// 新建表单
		if(url.indexOf("formManagement/newForm.do")>=0){
			return true;
		}
		if(url.indexOf("flowManagement/myFlow.do")>=0){
			return true;
		}
		
		// 审批
		if(url.indexOf("approval/selectDept.do")>=0){
			return true;
		}
		if(url.indexOf("approval/approvals.do")>=0){
			return true;
		}
		if(url.indexOf("approval/waitApproval.do")>=0){
			return true;
		}
		if(url.indexOf("approval/alreadyApproval.do")>=0){
			return true;
		}
		if(url.indexOf("approval/insertDept.do")>=0){
			return true;
		}
		if(url.indexOf("approval/queryAllDept.do")>=0){
			return true;
		}
		//发起审批
		if(url.indexOf("initiateMana/listAllModel.do")>=0){
			return true;
		}
		//新增审批
		if(url.indexOf("initiateMana/findModelByID.do")>=0){
			return true;
		}
        response.sendRedirect("/index.jsp");
        return false;
	}	
	
	//进入Handler方法之后，返回modelAndView之前执行
	//应用场景从modelAndView出发：将公用的模型数据(比如菜单导航)在这里传到视图，也可以在这里统一指定视图
	public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
	}
	 //执行Handler完成执行此方法
	//应用场景：统一异常处理，统一日志处理
	public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}
	
	public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if(StringUtil.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if(index != -1){
                return ip.substring(0,index);
            }else{
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if(StringUtil.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            return ip;
        }
        return request.getRemoteAddr();
    }
	public static String getuserbrowser(HttpServletRequest request) {
		//String Agent = request.getHeader("User-Agent");
		//StringTokenizer st = new StringTokenizer(Agent,";");
		//st.nextToken();
		//得到用户的浏览器名
		//String userbrowser = st.nextToken();
		//得到用户的操作系统名
		//String useros = st.nextToken();
		
		String agent=request.getHeader("User-Agent").toLowerCase();
		System.out.println("浏览器版本："+getBrowserName(agent));
		return getBrowserName(agent);
	}
	
	
	public static String getBrowserName(String agent) {
		  if(agent.indexOf("msie 7")>0){
		   return "ie7";
		  }else if(agent.indexOf("msie 8")>0){
		   return "ie8";
		  }else if(agent.indexOf("msie 9")>0){
		   return "ie9";
		  }else if(agent.indexOf("msie 10")>0){
		   return "ie10";
		  }else if(agent.indexOf("msie")>0){
		   return "ie";
		  }else if(agent.indexOf("opera")>0){
		   return "opera";
		  }else if(agent.indexOf("opera")>0){
		   return "opera";
		  }else if(agent.indexOf("firefox")>0){
		   return "firefox";
		  }else if(agent.indexOf("webkit")>0){
		   return "webkit";
		  }else if(agent.indexOf("gecko")>0 && agent.indexOf("rv:11")>0){
		   return "ie11";
		  }else{
		   return "Others";
		  }
	}
	
}
